﻿using Daemon.Middleware;
using Daemon.Model;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DaemonAPI.Hubs
{
	public class UserOrderHub : Hub
	{
		private readonly IHubContext<UserOrderHub> _hubContext;

		public UserOrderHub(IHubContext<UserOrderHub> hubContext)
		{
			_hubContext = hubContext;
		}

		public async Task SendMessage(string user, string message)
		{
			await Clients.All.SendAsync("ReceiveMessage", user, message);
		}

		public override Task OnConnectedAsync()
		{
			Connected();
			return base.OnConnectedAsync();
		}

		public async Task NotifyUserOrderChanged()
		{
			await _hubContext.Clients.Groups("daemon").SendAsync("AddNewOrder", null);
		}

		private void Connected()
		{
			Console.WriteLine(Context.ConnectionId);
			Groups.AddToGroupAsync(Context.ConnectionId, "daemon");
		}
	}
}
